<?PHP
session_start();

require("../../conexao/ConectaMysql.class.php");
require("../../conexao/ControleAcesso.class.php");

ControleAcesso::verificaSessao();
$ACE_SETid = $_SESSION['SETid'];
$ACE_PRIVid = $_SESSION['PRIVid'];

$conexao = new ConectaMysql();

if (isset($_POST['txtbuscar'])) {
	$txtbuscar = $_POST['txtbuscar'];

}else if (isset($_GET['txtbuscar'])) {
	$txtbuscar = $_GET['txtbuscar'];	
}else{
	$txtbuscar = "";	
}

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Helpdesk</title>
<link href="../../estrutura/css/datagrid/light/estilo.css" rel="stylesheet" type="text/css" media="screen" />
<link href="../../estrutura/css/estruturaTotal.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>
<div class="headerModulo"><img src="../../estrutura/imagens/ico_confirmacao.png" alt="" /> &nbsp;Registros Finalizados</div>
<div class="filtros" style="display: block;">
 <form id="pesquisar" method="post" action="formulario_finalizados.php" name="pesquisar">
      <fieldset>
	     <span class="linha">
           <label class="preField" for="tipoServico">Localizar por:</label>
           <input id="txtbuscar" name="txtbuscar" type="text" value="<?php echo $txtbuscar; ?>"  size="40" class="caixa_texto">
           <input name="buscar" type="submit" id="buscar" value="Buscar" class="caixa_texto">
	     </span>
      </fieldset>
 </form>
</div>
<div class="datagrid_conteudo" id="datagrid_conteudo">
  <form id="datagrid_form_menus" name="datagrid_form_menus" action="formulario.php" onsubmit="return false">
    <table class="datagrid">
          
		<?php 
	 
		if ($txtbuscar != '') {
 			$txtbuscar = addslashes($txtbuscar);
			$sql_WHERE_txtbuscar = "((tb_helpdesk.HELPtitulo like '%$txtbuscar%') OR
			 						 (tb_helpdesk.HELPtitulo like '%$txtbuscar%') OR
									 (tb_categoria.CATdescricao like '%$txtbuscar%') OR									 
									 (tb_setor.SETsigla like '%$txtbuscar%') OR
									 (tb_funcionario.FUNnome like '%$txtbuscar%') OR
									 (tb_funcionario.FUNsobrenome like '%$txtbuscar%')) AND ";
		} else {
			
			$sql_WHERE_txtbuscar = "";
		}
			
		$queryHelpdesk = "SELECT		
								tb_helpdesk.HELPid, 
								tb_helpdesk.HELPtitulo, 
								tb_categoria.CATdescricao, 
								tb_funcionario.FUNnome,
								tb_funcionario.FUNsobrenome,
								tb_setor.SETid,
								tb_setor.SETsigla,
								tb_gestao.GESTdatainicial,
								tb_gestao.GESTdatafinal
							
							FROM  tb_helpdesk
							
								INNER JOIN tb_categoria ON
								tb_helpdesk.HELP_CATid = tb_categoria.CATid
								
								INNER JOIN tb_gestao ON
								tb_helpdesk.HELPid = tb_gestao.GEST_HELPid
								
								INNER JOIN tb_situacao ON
								tb_gestao.GEST_SITid = tb_situacao.SITid
								
								INNER JOIN tb_funcionario ON
								tb_helpdesk.HELP_FUNid = tb_funcionario.FUNid
								
								INNER JOIN tb_acesso ON
								tb_acesso.ACE_FUNid = tb_funcionario.FUNid
								
								INNER JOIN tb_privilegio ON
								tb_privilegio.PRIVid = tb_acesso.ACE_PRIVid
								
								INNER JOIN tb_setor ON
								tb_funcionario.FUN_SETid = tb_setor.SETid
								
								INNER JOIN tb_resposta ON
								tb_resposta.RESP_HELPid = tb_helpdesk.HELPid
							
							WHERE ($sql_WHERE_txtbuscar (tb_situacao.SITid = '3') and 
														((tb_setor.SETid = '$ACE_SETid') or 
														($ACE_PRIVid = '1') or (tb_helpdesk.HELP_DEST_SETid = '$ACE_SETid')))
							
							ORDER BY tb_gestao.GESTdatainicial DESC";
								 
			 
		$quantreg = $conexao->rowCount($queryHelpdesk);	
		?>

        <tr>
        	<td colspan="9" class="alinCentro"><?php include("../../estrutura/paginacao.php"); ?></td>
        </tr>
        
        <tr class="header">
            <th width="11%" class=" alinCentro " style="">Funcionário</th>
            <th width="11%" class=" alinCentro " style="">Setor Remente</th>
   	    <th width="11%" class=" alinCentro " style="">Titúlo </th>
            <th width="11%" class=" alinCentro " style="">Categoria</th>
            <th width="12%" class=" alinCentro " style="">Responsável</th>
          <th width="13%" class=" alinCentro " style="">Setor Responsável</th>
        <th width="11%" class=" alinCentro " style="">Data Cadastro</th>
            <th width="12%" class=" alinCentro " style=""> Ultima Alteração</th>
          <th width="8%" class=" alinCentro " style="">Op&ccedil;&otilde;es</th>
        </tr>

		<?php
	
		$result_helpdesk = $conexao->executaQuery("$queryHelpdesk LIMIT $inicial, $numreg");
		  
		foreach($result_helpdesk as $row_helpdesk){ 
		
			$HELPid       		= $row_helpdesk["HELPid"];
			$HELPtitulo			= $row_helpdesk["HELPtitulo"];
			$CATdescricao		= $row_helpdesk["CATdescricao"];
			$FUNnome			= $row_helpdesk["FUNnome"];
			$SETsigla			= $row_helpdesk["SETsigla"];
			$FUNsobrenome		= $row_helpdesk["FUNsobrenome"];
			$GESTdatainicial	= $row_helpdesk["GESTdatainicial"];
			$GESTdatafinal		= $row_helpdesk["GESTdatafinal"];
			
			$destinatario = "SELECT * FROM tb_resposta 
						   INNER JOIN tb_funcionario ON 
						   tb_resposta.RESP_FUNid = tb_funcionario.FUNid									
						   INNER JOIN tb_setor ON 
						   tb_setor.SETid = tb_funcionario.FUN_SETid 
						   WHERE tb_resposta.RESP_HELPid = '$HELPid'";
			  
			$result_destinatario = $conexao->executaQuery($destinatario);
			
			foreach($result_destinatario as $row_dest){				
				$SETsigla_dest 		=  $row_dest['SETsigla'];
				$FUN_SETid_dest		=  $row_dest['SETid'];
				$FUNnome_dest		=  $row_dest['FUNnome'];
				$FUNsobrenome_dest 	=  $row_dest['FUNsobrenome'];
				
			}
		?>	
        <tr id="datagrid_menus_linha_0" class="">
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$FUNnome $FUNsobrenome"; ?></span></td>          
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$SETsigla"; ?></span></td>
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$HELPtitulo"; ?></span></td>
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$CATdescricao"; ?></span></td>           
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$FUNnome_dest $FUNsobrenome_dest"; ?></span></td>
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$SETsigla_dest"; ?></span></td>
            <td class=" alinEsquerda "><span class="texto style57 style59 style41"><?php echo "$GESTdatainicial"; ?></span></td>
            <td class=" alinCentro "><span class="texto style57 style59 style41"><?php echo "$GESTdatafinal"; ?></span></td>        
            <td class=" alinCentro ">
            <a title="Visualizar Resposta" class="botaoDatagrid visualizar" href="../resposta/visualizar_resposta.php?HELPid=<?php echo "$HELPid";?>"></a>&nbsp; 
            </td>
        </tr>
		<?php } ?>  
    </table>     
  </form>
 </div> 
</body>
</html>
